﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 9:34 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Miming.Model;

namespace Miming.Gui
{
	/// <summary>
	/// Description of PurchaseOrderForm.
	/// </summary>
	public partial class PurchaseOrderForm : BaseForm
	{
		public PurchaseOrderForm() : this(new PurchaseOrder())
		{
			
		}
		
		public PurchaseOrderForm(PurchaseOrder o)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			richTextBoxShipTo.Text = CompanySingleton.Instance.ShipToInformation;
			Model = o;
			listView1.Resize += delegate { columnHeader2.Width = listView1.Width - 22 - (columnHeader1.Width + columnHeader3.Width + columnHeader4.Width + columnHeader5.Width + columnHeader6.Width + columnHeader7.Width); };
			Util.SwfUtility.SetupComboBox(comboBoxVendor, SessionFacade.FindAllVendors(), "Id", "Name");
			Util.SwfUtility.SetupComboBox(comboBoxClass, SessionFacade.FindAllClasses(), "Id", "Name");
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			PurchaseOrder o = Model as PurchaseOrder;
		}
		
		void ButtonSaveAndCloseClick(object sender, EventArgs e)
		{
			Save();
			Close();
		}
		
		void Save()
		{
			PurchaseOrder o = Model as PurchaseOrder;
			o.Date = dateTimePickerDate.Value;
			o.PONo = textBoxPONo.Text;
			o.Vendor = (comboBoxVendor.DataSource as IList<Vendor>)[comboBoxVendor.SelectedIndex];
			o.Class = (comboBoxClass.DataSource as IList<Class>)[comboBoxClass.SelectedIndex];
			o.VendorDetails = richTextBoxVendor.Text;
			o.ShipTo = richTextBoxShipTo.Text;
			o.Message = textBoxMessage.Text;
			o.Memo = textBoxMemo.Text;
			SessionFacade.SaveOrUpdatePurchaseOrder(o);
		}
		
		void ButtonSaveAndClearClick(object sender, EventArgs e)
		{
			Save();
			buttonClear.PerformClick();
		}
		
		void ButtonClearClick(object sender, EventArgs e)
		{
			Model = new PurchaseOrder();
		}
		
		void ComboBoxVendorSelectedIndexChanged(object sender, EventArgs e)
		{
			Vendor v = (comboBoxVendor.DataSource as IList<Vendor>)[comboBoxVendor.SelectedIndex];
			if (v != null) {
				richTextBoxVendor.Text = v.Address;
			}
		}
		
		void ButtonNewClick(object sender, EventArgs e)
		{
			using (PurchaseOrderItemForm f = new PurchaseOrderItemForm()) {
				if (MainForm.Instance.AddDialog(f) == DialogResult.OK) {
					(Model as PurchaseOrder).AddItem(f.Model as PurchaseOrderItem);
				}
			}
		}
	}
}
